﻿@typeparam TModel

@if (Show)
{
    @if (IsDelta)
    {
        <span class="alert alert-warning"><strong>@property</strong></span>
    }
    else
    {
        <span class="alert alert-success">(same)</span>
    }
}

@code {
    // The TModel being edited.
    [Parameter]
    public TModel? Model { get; set; }

    // The TModel in the database.
    [Parameter]
    public TModel? DbModel { get; set; }

    // Returns the property to inspect.
    [Parameter]
    public Func<TModel, IComparable>? Property { get; set; }

    // Only show if concurrency conflict exists.
    private bool Show => Model != null && DbModel != null;

    // Highlight properties with different values.
    private bool IsDelta;

    private IComparable? property;

    protected override void OnInitialized()
    {
        if (Property is not null && DbModel is not null)
        {
            property = Property(DbModel);

            if (Model is not null)
            {
                IsDelta = !Property(Model).Equals(Property(DbModel));
            }
        }
    }
}
